/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pong;

import java.awt.Point;

enum Direction 
{

    UP_LEFT(-1, -1),
    UP_RIGHT(+1, -1),
    DOWN_LEFT(-1, +1),
    DOWN_RIGHT(+1, +1);
    private Point slope;

    Direction(int dx, int dy) 
    {
        slope = new Point(dx, dy);
    }

    Point getSlope() 
    {
        return new Point(slope); // defensive copy
    }
}

public class Ball 
{
    public static final int DEFAULT_SIZE = 20;
    private Point location;
    //Stores the initial coordinates of the Balsl to use for init() method
    private Point initLocation; 
    private Direction dir = Direction.UP_LEFT;
    private int size;

    /**
     * Constructs a Ball whose top left coordinate of the bounding Rectangle
     * is at the specified location Point, with a default size specified by the
     * Ball class.
     * @param location the specified top left Point of the bounding Rectangle of 
     * the Ball
     */
    public Ball(Point location) 
    {
        initLocation = new Point(location);
        this.location = location;
        size = DEFAULT_SIZE;
    }

    /**
     * Changes the Ball's coordinates based on the Ball's current Direction
     */
    public void move() 
    {
        Point slope = dir.getSlope();
        location.translate(slope.x, slope.y);
    }

    /**
     * Resets the Ball's location to the initial starting Point
     */
    public void init() 
    {
        this.location = new Point(initLocation);
    }

    /**
     * Gets the top left coordinate of the Ball's bounding Rectangle
     * @return the top left coordinate of the Ball's bounding Rectangle
     */
    public Point getCurrentLocation() 
    {
        return new Point(location); //defensive copy
    }

    /**
     * Sets the Direction of the Ball
     * @param dir the Direction to set
     */
    void setDirection(Direction dir) 
    {
        this.dir = dir;
    }

     /**
      * Gets the current Direction of the Ball
      * @return the Direction of the Ball
      */
    public Direction getDirection() 
    {
        return dir;
    }

    /**
     * Changes the Direction of the Ball based on a collision that will change
     * the x Direction of the Ball. For example if Ball's current Direction 
     * is UP_RIGHT after a call to verticalBounce() the Direction will be
     * UP_LEFT
     */
    public void verticalBounce() 
    {
        if (dir == Direction.UP_LEFT) 
        {
            dir = Direction.UP_RIGHT;
        } 
        else if (dir == Direction.UP_RIGHT) 
        {
            dir = Direction.UP_LEFT;
        } 
        else if (dir == Direction.DOWN_LEFT) 
        {
            dir = Direction.DOWN_RIGHT;
        } 
        else 
        {
            dir = Direction.DOWN_LEFT;
        }
    }

    /**
     * Changes the Direction of the Ball based on a collision that will change
     * the y Direction of the Ball. For example if Ball's current Direction 
     * is UP_RIGHT after a call to horizontalBounce() the Direction will be
     * DOWN_RIGHT
     */
    public void horizontalBounce() 
    {
        if (dir == Direction.UP_LEFT) 
        {
            dir = Direction.DOWN_LEFT;
        } 
        else if (dir == Direction.UP_RIGHT) 
        {
            dir = Direction.DOWN_RIGHT;
        } 
        else if (dir == Direction.DOWN_LEFT) 
        {
            dir = Direction.UP_LEFT;
        } 
        else 
        {
            dir = Direction.UP_RIGHT;
        }
    }

    /**
     * Gets the size of the Ball
     * @return the size
     */
    public int getSize() 
    {
        return size;
    }

    /**
     * Sets the size of the Ball
     * @param size the size to set
     */
     void setSize(int size) 
     {
        this.size = size;
    }
}
